﻿ <?php
/**********************************************************
** Lime Manager
** Thomas Knobloch & Clement Troesch
** Manager Ajax Users View
***********************************************************/
?>
<?php include_once "../../model/DevelopperModel.php"; ?>
<?php include_once "../../model/SessionModel.php"; ?>
<?php include_once "../../model/DevelopperModel.php"; ?>
<?php session_start(); ?>

<div class="section_title" id="objects"><h1> Déclencheurs </h1></div>
<div class="section_info">
	Affichage des déclencheurs et possibilité de sauvegarder les commandes de mises en place. 
</div>

<div class="article">

<div class="section_title_small_blue"><h4>Sélectionnez un déclencheur</h4></div>

<table class="tableQueryCenter">
	<tr>
		<th>ID</th>
		<th>Nom</th>
		<th>Création</th>
		<th>Utilisation</th>
		<th>Namespace</th>
		<th>Affichage</th>
	</tr>
<?php
	$nbRow = DevelopperModel::getObjects($_SESSION['session']->currentConnection, $result, 'TRIGGER', $columns);
	
	if(isset($_GET['selected_name']))
		$selectedTrigger = $_GET['selected_name'];
	
	foreach ($result as $row) {
		echo "<tr>";
		echo "<td>".$row['ID']."</td>\n";
		echo "<td>".$row['NOM']."</td>\n";
		echo "<td>".$row['CREATION']."</td>\n";
		echo "<td>".$row['UTILISATION']."</td>\n";
		echo "<td>".$row['NAMESPACE']."</td>\n";
		
		if(isset($selectedTrigger) && $selectedTrigger == $row['NOM'])
		{
			?><td><img src="img/check.png" alt="check" /></td><?php
		}
		else
		{
			?><td><a href="#objects"><img src="img/find.png" alt="zoom" onClick="displayTriggers('<?php echo $row['NOM'] ?>')" /></a></td><?php
		}
		
		
		echo"</tr>";
	}
?>
</table>

<br />

<?php

if(isset($selectedTrigger))
{
	?>
	
	<div class="section_title_small_green"><h4><?php echo $selectedTrigger ?></h4></div>
	
	<div class="section_title_small_blue"><h4>Commande SQL</h4></div>
	
	<div class="sql_code">
	<pre class="sh_sql">
		<?php
		echo "\n";
		$nbRow = DevelopperModel::getCreationQuery($_SESSION['session']->currentConnection, $query, 'TRIGGER', $selectedTrigger);
		if($_SESSION['session']->currentConnection->type == "ORACLE")
			$sql = $query[0]['QUERY'];
		else if($_SESSION['session']->currentConnection->type == "MYSQL")
			$sql = $query[0]['SQL Original Statement'];
			
		echo htmlentities($sql);
		?>
	</pre>
	</div>
	
	<a href="view/main/DownloadView.php?name=Save.sql&text=<?php echo urlencode($sql) ?>" target="_blank"><input type="button" value="Télécharger" /></a>
	
	<div class="section_title_small_blue"><h4>Code</h4></div>
	
	<div class="sql_code">
	<pre class="sh_sql">
		<?php
		echo "\n";
		$nbRow = DevelopperModel::getFunctionCode($_SESSION['session']->currentConnection, $code, 'TRIGGER', $selectedTrigger);
		foreach ($code as $line) {
			echo htmlentities($line['TEXT']);
		}
		?>
	</pre>
	</div>

<?php
}

?>

</div>

<script type="text/javascript" src="js/sql/shjs.js"></script>
<script type="text/javascript" src="js/sql/sh_sql.js"></script>
<script type="text/javascript">

	//JQuery main entry point
	 $(document).ready(function() {
		//Highlight query
		sh_highlightDocument();
	 });
</script>



